package com.zhny.aiot.soil.plugin;

import com.zhny.aiot.plugin.ICenterFacilities;
import com.zhny.aiot.plugin.IDeviceFactory;
import com.zhny.aiot.plugin.modbus.ModemEngine;
import com.zhny.aiot.plugin.modbus.device.ModemConfig;
import com.zhny.aiot.plugin.modbus.device.modbus.ModbusNumDevice;
import com.zhny.aiot.plugin.modbus.handler.model.ModemRequestMessage;
import com.zhny.aiot.plugin.modbus.handler.model.ModemResponseMessage;
import com.zhny.aiot.plugin.model.DeviceEntity;
import com.zhny.aiot.plugin.model.TaskEntity;
import com.zhny.aiot.soil.plugin.model.SoilStoreEntry;

public class SoilDevice extends ModbusNumDevice<SoilStoreEntry> {

    public SoilDevice(ModemEngine engine, IDeviceFactory<ModemConfig> factory, ICenterFacilities centerFacilities, DeviceEntity entity) {
        super (engine, factory, centerFacilities,entity);
    }

    @Override
    public SoilStoreEntry decode(ModemResponseMessage modemResponseMessage) {
        SoilStoreEntry entry = new SoilStoreEntry ();
        entry.setWatercontent (modemResponseMessage.getData ().readShort ()/10.0);
        entry.setTemperature (modemResponseMessage.getData ().readShort ()/10.0);
        entry.setConductivity (modemResponseMessage.getData ().readUnsignedShort ());
        entry.setPh(modemResponseMessage.getData().readUnsignedShort()/10.0);
        entry.setSalinity (modemResponseMessage.getData ().readUnsignedShort ());
        return entry;
    }

    @Override
    public boolean checkError(ModemResponseMessage modemResponseMessage) {
        return modemResponseMessage.getData ().readableBytes () == 10;
    }

    @Override
    protected ModemRequestMessage getCommand(TaskEntity taskEntity) {
        return getFactory ().getRequestMessage (getConfig ());
    }

    @Override
    protected void disposable() {

    }
}
